package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.InitRoomJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoleTypeEntity;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.model.parameter.dto.RoomParameterDto;
import com.cicc.beergamevirtualsimulationsystem.model.userType.RoleTypeParameterDto;
import com.cicc.beergamevirtualsimulationsystem.service.ParameterService;
import com.cicc.beergamevirtualsimulationsystem.service.RoomService;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Component
public class InitRoomJsonMessageHandler implements JsonMessageHandler<InitRoomJsonWebSocketMessage> {

    @Resource
    private ParameterService parameterService;



    @Override
    public void handle(WebSocketSession session, InitRoomJsonWebSocketMessage message) {
        Object roomId = session.getAttributes().get("roomId");
        String masterId = (String)session.getAttributes().get("userId");
        System.out.println("--------------" + masterId);
        if (roomId != null) {
            // TODO: 处理初始化房间消息
            RoomParameterDto roomParameterDto = parameterService.queryParameterByRoomId(roomId.toString());

            System.out.println("--------------" + roomParameterDto);

            //获取对应角色的人数限制
            List<RoleTypeParameterDto> roleTypeParameterList = roomParameterDto.getRoleTypeParameterList();

            Map<String, Integer> collect = roleTypeParameterList.stream().collect(Collectors.toMap(
                    entity -> String.valueOf(entity.getRoleType()), // key 是 String 类型的 roleType
                    RoleTypeParameterDto::getRoleNum                   // value 是 roleNum
            ));

            //角色类型id和房间映射
            Map<String, String> mapping = roleTypeParameterList.stream().collect(Collectors.toMap(entity -> String.valueOf(entity.getRoleType()), RoleTypeParameterDto::getId));


            //System.out.println("映射--------------"+mapping);

            RoomInfoManager.initializeRoom(roomId.toString(), roomParameterDto.getCurrentRoomParameter().getGameTime(), roomParameterDto.getCurrentRoomParameter().getCycles(), collect,mapping,masterId);

            WebSocketMessageSender.send(session, new InitRoomJsonWebSocketMessage("创建成功"));

        } else {
            // TODO: 处理错误
        }

    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.INIT_GAME_ROOM.getValue();
    }

    @Override
    public Class<InitRoomJsonWebSocketMessage> getMessageClass() {
        return InitRoomJsonWebSocketMessage.class;
    }
}
